package fsnotify

import (
	"os"
	"sync"
	"syscall"
)

//go:build windows
// +build windows
type Watcher struct {
	Events  chan Event
	Errors  chan error
	isCloed bool           // set to true when close is first called
	mu      sync.Mutex     //map access
	port    syscall.Handle // handle to completion port
	watches watchMap       // map of watches key 1-number
	input   chan *input    // inputs to reader are sent on this channel
	quit    chan chan<- error
}

func NewWatcher() (*Watcher, error) {
	port, e := syscall.CreateIoCompletionPort(syscall.InvalidHandle, 0, 0, 0)
	if e != nil {
		return nil, os.NewSyscallError("CreateCompletionPort", e)
	}
	w := &Watcher{
		port:    port,
		watches: make(watchMap),
		input:   make(chan *input, 1),
		Events:  make(chan Event, 50),
		Errors:  make(chan error),
		quit:    make(chan chan<- error, 1),
	}
	go w.readEvents()
	return w, nil
}
